var t = getApp();

Component({
    properties: {
        options: {
            type: Object,
            value: {}
        },
        route: {
            type: String,
            value: ""
        }
    },
    data: {
        ing: !1,
        show: !1,
        saving: !1
    },
    methods: {
        close: function() {
            this.setData({
                ing: !1,
                show: !1,
                saving: !1
            });
        },
        showqr: function(s) {
            var o = this;
            if (!this.data.ing) if (this.data.auths || wx.getSetting({
                success: function(t) {
                    "getSetting:ok" === t.errMsg && o.setData({
                        auths: t.authSetting
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            }), this.data.show) this.setData({
                show: !1
            }); else if (this.data.qr) this.setData({
                show: !0
            }); else {
                var e = [];
                for (var a in this.properties.options) "q" !== a && "scene" !== a && e.push(a + "=" + this.properties.options[a]);
                this.setData({
                    ing: !0
                }), console.log("生成二维码", this.properties.route, e), t.request({
                    url: t.server + "/wxOpenApi/getPageQrCode",
                    method: "POST",
                    data: {
                        token: t.token,
                        page: this.properties.route,
                        scene: e.join("&")
                    },
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    success: function(t) {
                        o.setData({
                            ing: !1,
                            show: !0,
                            qr: t.data.result.url
                        });
                    },
                    fail: function(t) {
                        o.setData({
                            ing: !1
                        });
                    }
                });
            }
        },
        saveToAlbum: function(t) {
            var s = this;
            this.data.saving || (this.setData({
                saving: !0
            }), wx.getSetting({
                success: function(t) {
                    t.authSetting["scope.writePhotosAlbum"] ? s.doSaveToAlbum() : wx.authorize({
                        scope: "scope.writePhotosAlbum",
                        success: function() {
                            s.doSaveToAlbum();
                        },
                        fail: function() {
                            this.setData({
                                saving: !1
                            }), wx.showToast({
                                title: "保存失败[0]"
                            });
                        }
                    });
                },
                fail: function() {
                    this.setData({
                        saving: !1
                    }), wx.showToast({
                        title: "保存失败[1]"
                    });
                }
            }), console.log(t));
        },
        doSaveToAlbum: function() {
            var t = this;
            wx.downloadFile({
                url: this.data.qr,
                success: function(s) {
                    console.log("下载文件：success"), console.log(s), t.setData({
                        saving: !1
                    }), wx.saveImageToPhotosAlbum({
                        filePath: s.tempFilePath,
                        success: function(s) {
                            console.log("保存图片：success"), wx.showToast({
                                title: "保存成功"
                            }), t.setData({
                                show: !1
                            });
                        },
                        fail: function(t) {
                            wx.showToast({
                                title: "保存失败[2]"
                            }), console.log("保存图片：fail"), console.log(t);
                        }
                    });
                },
                fail: function(s) {
                    t.setData({
                        saving: !1
                    }), console.log("下载文件：fail"), console.log(s), wx.showToast({
                        title: "保存失败[3]"
                    });
                }
            });
        }
    }
});